import {
  request,
} from '../../utils/requestUtils';
Page({
  data: {
    sort:1,
    touchDotY: 0,//触摸时的原点 
    touchMoveY:0,// 触摸结束位置
    pause:true,
    videoContext:'',
    carUrl: 'https://proxyimg.sucai999.com/preimg/CF8E2D/700/CF8E2D/b3/b36002710732cdd57c60623c475d9396.png?x-oss-process=format,webp',
    banner: {},
  },

  onLoad(options) {
    // 查询banner
    this.setData({
      sort:options.attr2
    })
    this.getBySort(options.attr2)
  },
  async getBySort() {
    const result = await request({
      url: `/bus/customer/banner/getBySort/${this.data.sort}`,
      method: "GET",
      data: {}
    })
    if(result.data.code == 200){
      this.setData({
        banner: result.data.data,
        sort: result.data.data.attr2
      })
      console.log('banner', this.data.banner)
      this.startUp()
    }
  },
  // 进页面时播放视频
  startUp() {
    // 获取video节点
    this.setData({
      pause: true
    })
    this.videoContext = wx.createVideoContext('video')
  },
  playEnd(){
    this.setData({
      pause: true
    })
    this.videoContext.pause();
  },
  togglePlay(e) {
    console.log('ces', e)
    this.setData({
      pause: !this.data.pause
    })
    if(this.data.pause){
      this.videoContext.pause();
    }else{
      this.videoContext.play()
    }

  },
  // 下单
  toOrder(e){
    console.log('下单', e)
    let component = this.selectComponent('#addOrder')
    if (component) {
      component.addOrderShow(this.data.banner.lines);
    }
  },
  // 触摸开始
  touchStart(e){
    this.setData({
      touchDotY:e.touches[0].pageY
    })
  },
  // 触摸中
  touchMove(e){
    this.setData({
      touchMoveY:e.touches[0].pageY
    })
  },
  // 触摸结束
  touchEnd(e){
    // 计算距离
    if(this.data.touchDotY == 0 || this.data.touchMoveY == 0)return
    let direction = this.data.touchMoveY-this.data.touchDotY;
    if(Math.abs(direction)>200){
      if(direction>0){
        this.setData({
          sort: parseInt(this.data.sort) - 1
        })
        this.getBySort()
      }else{
        this.setData({
          sort: parseInt(this.data.sort) +1
        })
        this.getBySort()
      }
    }
  }
})